import {getWy,getPlatform} from '@/uni_modules/wy-core/context'
import {_resList,_resModel,_resAction,getModel,getList,getData,doAction} from '../utils.js'
const that={
	pub_list(option){
		let {ac}=option
		return getList(ac,option)
	},
	kh_list(option){
		let {ac}=option
		return getList(ac,option)
	},
	pub_action(option){
		let {ac}=option
		return doAction(ac,option)
	},
	kh_action(option){
		let {ac}=option
		return doAction(ac,option)
	},
	pub_model(option){
		let {ac}=option
		return getModel(ac,option)
	},
	kh_model(option){
		let {ac}=option
		return getModel(ac,option)
	},
	pub_data(option){
		let {ac}=option
		return getData(ac,option)
	} ,
	kh_data(option){
		let {ac}=option
		return getData(ac,option)
	},
	pub_checkUpdate(option){
		console.log('====--------=pub_checkUpdate')
		let {ac}=option
		ac=ac?ac:'checkUpdate'
		var wy=getWy()
		option.data={
			// #ifdef APP-PLUS
			appid: plus.runtime.appid,
			appVersion: plus.runtime.version,
			wgtVersion: wy.wgtVersion? wy.wgtVersion:plus.runtime.version,
			// #endif
			package:wy.config.App.package
		}
		return getData(ac,option)
	},
	pub_checkUpdate_(result){ 
		// #ifndef APP-PLUS
		if(result&&result.hasOwnProperty('code')&&result.hasOwnProperty('msg')){
			return result
		}
		return {code:0,data:result,msg:''}
		// #endif
		if(result){
			let {code,url,is_silently,type}=result
			if(code>0){
				if (is_silently) {
					uni.downloadFile({
						url: url,
						success: rs => {
							if (rs.statusCode == 200) {
								// 下载好直接安装，下次启动生效
								plus.runtime.install(rs.tempFilePath, {
									force: false
								});
							}
						}
					});
					return;
				}
				const PACKAGE_INFO_KEY = '__package_info__'
				uni.setStorageSync(PACKAGE_INFO_KEY,result)
				uni.navigateTo({
					url: `/uni_modules/uni-upgrade-center-app/pages/upgrade-popup?local_storage_key=${PACKAGE_INFO_KEY}`,
					fail: (err) => {
						console.error('更新弹框跳转失败', err)
						uni.removeStorageSync(PACKAGE_INFO_KEY)
					}
				})
			}
		}
		if(result&&result.hasOwnProperty('code')&&result.hasOwnProperty('msg')){
			return result
		}
		else{
			return {code:0,data:result,msg:''}
		}
	}
};
export default that
